import { Controller, Get, Post, Body, Patch, Param, Delete, Req } from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import R from 'src/common/dto/R.dto';

@Controller('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Post()
  async save(@Body() createMenuDto: CreateMenuDto) {
    await this.menuService.create(createMenuDto);
    return R.success('保存成功！');
  }

  @Get()
  async findAll() {
    const menus = await this.menuService.findAll();
    return R.success(menus, '查询成功');
  }

  @Get('findAllWithPermissions')
  async findAllWithPermissions(@Req() req: { user: { username: string; sub: number } }) {
    const menus = await this.menuService.findAllWithPermissions(req.user.sub);
    return R.success(menus, '查询成功');
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    return R.success(await this.menuService.findOne(+id));
  }

  @Delete(':menuId')
  async remove(@Param('menuId') menuId: string) {
    await this.menuService.remove(+menuId);
    return R.success('删除成功！');
  }
}
